package com.example.juc.demo;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //1.runnable
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("线程："+Thread.currentThread().getName()+"正在运行。。");
            }
        };
        Thread t1 = new Thread(r1,"runnable");
        t1.start();

        //runnable简化为lambda
        Thread t2 = new Thread(() -> System.out.println("线程："+Thread.currentThread().getName()+"正在运行。。"),"t2");
        t2.start();



        //2.callable
        FutureTask<String> task = new FutureTask<>(() -> {
            System.out.println("线程："+Thread.currentThread().getName()+"正在运行。。");
            Thread.sleep(3000);
            return "callable";
        });
        Thread t3 = new Thread(task,"callable");
        t3.start();
        System.out.println("线程："+Thread.currentThread().getName()+"的运行结果为："+task.get());

    }


}

